package br.com.andre.neo4j.model;

import java.util.ArrayList;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;
import org.neo4j.kernel.Traversal;
import org.neo4j.tooling.GlobalGraphOperations;

public class ActorDAO {
	private Connection conn;
	public ActorDAO() {
		this.conn = new Connection();
	}
	
	public void createActor(ActorTO actor) {
		GraphDatabaseService graph = conn.newConnection();
		Index<Node> index = graph.index().forNodes("actor");
		Node node = index.get("name", actor.getName()).getSingle();
		
		if (node == null) {
			Transaction tx = graph.beginTx();
			try {
				node = graph.createNode();
				node.setProperty("name", actor.getName());
				node.setProperty("sex", actor.getGender());
				index.add(node, "name", actor.getName());
				tx.success();
			} catch (Exception e) {
				tx.failure();
			} finally {
				tx.finish();
			}
		}
		graph.shutdown();
	}

}
